namespace DiLu.Expansion.File.Configs;

/// <summary>
///     保存文件流配置
/// </summary>
public abstract record SaveFileStreamConfig
{
    /// <summary>
    ///     文件流保存配置
    /// </summary>
    /// <param name="rootDirectory">根目录</param>
    /// <param name="fileGroupName">文件分组名称</param>
    /// <param name="fileStream">文件流</param>
    /// <param name="fileExtension">文件扩展名</param>
    protected SaveFileStreamConfig(string rootDirectory, string fileGroupName, Stream fileStream, string fileExtension)
    {
        ArgumentNullException.ThrowIfNull(fileStream, nameof(fileStream));
        if (fileStream.Length <= 0) throw new EndOfStreamException("文件流大小不能为0");
        if (!fileStream.CanRead) throw new ArgumentException("文件流不能读取");

        ArgumentException.ThrowIfNullOrWhiteSpace(rootDirectory, nameof(rootDirectory));
        rootDirectory = rootDirectory.Replace('\\', '/').Trim();
        var rootPath = Path.GetPathRoot(rootDirectory);
        ArgumentException.ThrowIfNullOrWhiteSpace(rootPath, nameof(rootPath));
        if (new DriveInfo(rootPath).DriveType == DriveType.Unknown) throw new ArgumentException("目标文件夹不是磁盘根目录");

        FileStream = fileStream;
        RootDirectory = rootDirectory;
        FileGroupName = $"{fileGroupName?.Trim()}";
        FileExtension = fileExtension.Trim();
        FileStream.Position = 0;
    }

    /// <summary>
    ///     文件流
    /// </summary>
    public Stream FileStream { get; }

    /// <summary>
    ///     根目录
    /// </summary>
    public string RootDirectory { get; }

    /// <summary>
    ///     文件分组名称
    /// </summary>
    public string FileGroupName { get; }

    /// <summary>
    ///     文件扩展名
    /// </summary>
    public string FileExtension { get; }
}